package mystack;

import javax.sound.midi.Soundbank;
import java.util.*;

public class Solution {
    /**
     * 20 有效的括号
     * @param s
     * @return
     */
    public static boolean isValid(String s){
        int n=s.length();
        if(n%2==1){
            return false;
        }
        Map<Character,Character> pairs= new HashMap<>();
        pairs.put(')','(');
        pairs.put(']','[');
        pairs.put('}','{');
        Deque<Character> stack = new LinkedList<>();
        for (int i = 0; i <n; i++) {
            char ch = s.charAt(i);
            if(pairs.containsKey(ch)){

                Character peek = stack.peek();
                Character value = pairs.get(ch);
                if(stack.isEmpty()|| peek != value) {
                    return false;
                }
                stack.pop();
            }else{
                stack.push(ch);
            }
        }
        return stack.isEmpty();
    }

    /**
     *  + 前两相加
     *  C 删除前
     *  D 前次计分Double
     *  682 棒球比赛计分
     * @param ops
     * @return
     */
    public static int calPoints(String[] ops){
        int ret =0;
        List<Integer> list = new ArrayList<>();
        for (String op : ops) {
            int size = list.size();
            if(op.equals("+")){
                int pre = list.get(size - 1);
                int prepre = list.get(size - 2);
                int curr = pre + prepre;
                list.add(curr);
                ret=ret+curr;
            }else if(op.equals("D")){
                int pre = list.get(size - 1);
                int curr = pre*2;
                list.add(curr);
                ret=ret+curr;
            }else if(op.equals("C")){
                Integer remove = list.remove(size - 1);
                ret=ret-remove;
            }else {
                int i = Integer.parseInt(op);
                list.add(i);
                ret=ret+i;
            }


        }
        return ret;
    }

    public static void main(String[] args) {
        System.out.println(isValid("({)}[]"));
        System.out.println(calPoints(new String[]{"5","2","C","D","+"}));
    }
}
